function EditAssistant() {
}

EditAssistant.prototype.setup = function() {
	Mojo.Log.info("==================================================");
	Mojo.Log.info("| Edit Scene");
	Mojo.Log.info("| EditAssistant.setup()");
	Mojo.Log.info("--------------------------------------------------");
	
	// Create an array of day names that will be used to dynamically populate HTML and model objects.
	this.days = [$L("Sunday"), $L("Monday"), $L("Tuesday"), $L("Wednesday"), $L("Thursday"), $L("Friday"), $L("Saturday")];
	
	// Create arrays to hold the open and close times for each day of the week.
	this.timeOpen = [];
	this.timeClose = [];
	
	// Setup the description text field widget.
	this.descriptionAttributes = {
		hintText: $L("Business Name or Description"),
		multiline: false,
		enterSubmits: false,
		autoFocus: true,
		textCase: Mojo.Widget.steModeTitleCase,
		focusMode: Mojo.Widget.focusSelectMode
	}
	
	this.descriptionModel = {
		value: ""
	}
	
	this.controller.setupWidget("description", this.descriptionAttributes, this.descriptionModel);
	
	// Setup the location text field widget.
	this.locationAttributes = {
		hintText: $L("Business Location"),
		multiline: false,
		enterSubmits: false,
		textCase: Mojo.Widget.steModeTitleCase,
		focusMode: Mojo.Widget.focusSelectMode
	}
	
	this.locationModel = {
		value: ""
	}
	
	this.controller.setupWidget("location", this.locationAttributes, this.locationModel);
	
	// Setup the phone number text field widget.
	this.phoneAttributes = {
		hintText: $L("Phone Number"),
		multiline: false,
		enterSubmits: false,
		modifierState: Mojo.Widget.numLock,
		focusMode: Mojo.Widget.focusSelectMode
	
	}
	
	this.phoneModel = {
		value: ""
	}
	
	this.controller.setupWidget("phone", this.phoneAttributes, this.phoneModel);
	
	// Setup the schedule type list selector widget.
	this.scheduleTypeAttributes = {
		labelPlacement: Mojo.Widget.labelPlacementRight,
		label: $L("Schedule Type"),
		choices: [{
			label: $L("Custom"),
			value: 1
		}, {
			label: $L("Always Open"),
			value: 2
		}, {
			label: $L("By Appointment"),
			value: 3
		}]
	}
	
	this.scheduleTypeModel = {
		value: 1,
		disabled: false
	}
	
	this.controller.setupWidget("scheduleType", this.scheduleTypeAttributes, this.scheduleTypeModel);
	
	// Setup the drawer that will contain all days of the week.
	this.controller.setupWidget("customScheduleDrawer", {
		unstyled: true
	}, {
		open: true
	});
	
	// Loop over each day of the week: Sunday = Day 1, Saturday = Day 7
	for (var thisDayIndex = 1; thisDayIndex <= 7; thisDayIndex++) {
		// This array index will be used to store the model (date/time value) for each
		// OPEN time picker.  The time picker widget requires an existing, unique, date object.
		this.timeOpen[thisDayIndex] = new Date();
		this.timeOpen[thisDayIndex].setFullYear(1984, 0, thisDayIndex);
		this.timeOpen[thisDayIndex].setHours(8, 0, 0, 0);
		
		// This array index will be used to store the model (date/time value) for each
		// CLOSE time picker.  The time picker widget requires an existing, unique, date object.
		this.timeClose[thisDayIndex] = new Date();
		this.timeClose[thisDayIndex].setFullYear(1984, 0, thisDayIndex);
		this.timeClose[thisDayIndex].setHours(17, 0, 0, 0);
		
		// Setup the open/closed toggle widget for each day
		this.controller.setupWidget("availableToggleDay" + thisDayIndex, {
			trueLabel: $L("Open"),
			trueValue: true,
			falseLabel: $L("Closed"),
			falseValue: false
		}, {
			value: true,
			disabled: false,
			dayNumber: thisDayIndex,
			dayName: this.days[thisDayIndex - 1]
		});
		
		// Setup the drawer widget for each day
		this.controller.setupWidget("drawerDay" + thisDayIndex, {
			unstyled: true
		}, {
			open: true
		});
		
		// Setup the open-hour time picker widget for each day
		this.controller.setupWidget("timeOpenPickerDay" + thisDayIndex, {
			label: $L("Opens"),
			minuteInterval: 15
		}, {
			time: this.timeOpen[thisDayIndex],
			dayNumber: thisDayIndex
		});
		
		// Setup the close-hour time picker widget for each day
		this.controller.setupWidget("timeClosePickerDay" + thisDayIndex, {
			label: $L("Closes"),
			minuteInterval: 15
		}, {
			time: this.timeClose[thisDayIndex],
			dayNumber: thisDayIndex
		});
	}
};

EditAssistant.prototype.activate = function(event) {
	Mojo.Log.info("==================================================");
	Mojo.Log.info("| Edit Scene");
	Mojo.Log.info("| EditAssistant.activate()");
	Mojo.Log.info("--------------------------------------------------");
	
	// Create a handler to the custom schedule drawer function.
	// Listen for a value change in the schedule type list selector.
	// If the list value is changed, call the toggleCustomScheduleDrawer function.
	this.toggleCustomScheduleDrawerHandler = this.toggleCustomScheduleDrawer.bindAsEventListener(this);
	this.controller.listen("scheduleType", Mojo.Event.propertyChanged, this.toggleCustomScheduleDrawerHandler);
	
	// Create a handler to the toggle day drawer function.
	this.toggleDayDrawerHandler = this.toggleDayDrawer.bindAsEventListener(this);
	
	// Create a handler to the time picker change function.
	this.handleTimePickerChangeHandler = this.handleTimePickerChange.bindAsEventListener(this)
	
	for (var thisDayIndex = 1; thisDayIndex <= 7; thisDayIndex++) {
		// Listen for a change in the toggle for each day of the week.
		// If the widget is toggled, call the toggleDayDrawer function.
		this.controller.listen("availableToggleDay" + thisDayIndex, Mojo.Event.propertyChanged, this.toggleDayDrawerHandler);
		
		// Listen for a change in the OPEN time picker for each day of the week.
		// If the widget is toggled, call the handleTimePickerChange function.
		this.controller.listen("timeOpenPickerDay" + thisDayIndex, Mojo.Event.propertyChange, this.handleTimePickerChangeHandler);
		
		// Listen for a change in the CLOSE time picker for each day of the week.
		// If the widget is toggled, call the handleTimePickerChange function.
		this.controller.listen("timeClosePickerDay" + thisDayIndex, Mojo.Event.propertyChange, this.handleTimePickerChangeHandler);
		
		// Set the initial info text value for each day of the week.
		updateDayInfoText(this.controller.get("infoTextDay" + thisDayIndex), this.timeOpen[thisDayIndex], this.timeClose[thisDayIndex]);
	}
};

EditAssistant.prototype.deactivate = function(event) {
	Mojo.Log.info("==================================================");
	Mojo.Log.info("| Edit Scene");
	Mojo.Log.info("| EditAssistant.deactivate()");
	Mojo.Log.info("--------------------------------------------------");
	
	this.controller.stopListening("scheduleType", Mojo.Event.propertyChanged, this.toggleCustomScheduleDrawerHandler);
	
	for (var thisDayIndex = 1; thisDayIndex <= 7; thisDayIndex++) {
		this.controller.stopListening("availableToggleDay" + thisDayIndex, Mojo.Event.propertyChanged, this.toggleDayDrawerHandler);
		this.controller.stopListening("timeOpenPickerDay" + thisDayIndex, Mojo.Event.propertyChange, this.handleTimePickerChangeHandler);
		this.controller.stopListening("timeClosePickerDay" + thisDayIndex, Mojo.Event.propertyChange, this.handleTimePickerChangeHandler);
	}
};

EditAssistant.prototype.cleanup = function(event) {
	Mojo.Log.info("==================================================");
	Mojo.Log.info("| Edit Scene");
	Mojo.Log.info("| EditAssistant.cleanup()");
	Mojo.Log.info("--------------------------------------------------");
};

EditAssistant.prototype.toggleCustomScheduleDrawer = function(event) {
	var isOpen = "";
	
	switch (event.value.toString()) {
		case "1":
			isOpen = true;
			break;
		case "2":
			isOpen = false;
			break;
		case "3":
			isOpen = false;
			break;
	}
	this.controller.setWidgetModel("customScheduleDrawer", {
		open: isOpen
	});
};

EditAssistant.prototype.toggleDayDrawer = function(event) {
	var toggledDrawer = "drawerDay" + event.model.dayNumber;
	this.controller.setWidgetModel(toggledDrawer, {
		open: event.value
	});
};

EditAssistant.prototype.handleTimePickerChange = function(event) {
	var timeOpenObj = this.timeOpen[event.model.dayNumber];
	var timeCloseObj = this.timeClose[event.model.dayNumber];
	
	updateDayTimeModel(timeOpenObj, timeCloseObj);
	updateDayInfoText(this.controller.get("infoTextDay" + event.model.dayNumber), timeOpenObj, timeCloseObj);
};

